package com.itheima.case2.service.impl;

import com.itheima.case2.dao.UserMapper;
import com.itheima.case2.dao.UserRoleMapper;
import com.itheima.case2.pojo.po.User;
import com.itheima.case2.pojo.vo.AddUser;
import com.itheima.case2.pojo.vo.PageResult;
import com.itheima.case2.pojo.vo.QueryPageBean;
import com.itheima.case2.pojo.vo.UpdateUser;
import com.itheima.case2.service.UserService;
import com.itheima.case2.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {

    //分页查询用户和对应角色信息
    @Override
    public PageResult queryUserAndRoleByPage(QueryPageBean pageBean) {
        //获取会话对象
        SqlSession session = SqlSessionUtil.getSession(false);
        //获取接口代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        //调用方法
        Integer startIndex = pageBean.getOffset();
        Integer pageSize = pageBean.getPageSize();
        Map<String,String> queryParams = pageBean.getQueryParams();
        String username = queryParams.get("username");
        List<User> list = mapper.queryUserAndRoleByPage(startIndex,pageSize,username);
        //查询User表数据条数
        Long total = mapper.queryUserCount(username);
        //封装到PageResult中，传给前端
        PageResult pageResult = new PageResult(total, list);
        //释放资源
        session.close();
        return pageResult;
    }

    //修改用户和对应角色信息
    @Override
    public void updateUserAndRole(UpdateUser updateUser) {
        //获取会话对象
        SqlSession session = SqlSessionUtil.getSession();
        //获取接口代理对象
        UserMapper userMapper = session.getMapper(UserMapper.class);
        UserRoleMapper userRoleMapper = session.getMapper(UserRoleMapper.class);
        //获取要修改的数据
        User user = new User();
        String userId = updateUser.getId().toString();
        user.setId(userId);
        user.setUsername(updateUser.getUsername());
        user.setPassword(updateUser.getPassword());
        user.setEmail(updateUser.getEmail());
        List<String> roleIds = updateUser.getRoleIds();
        //调用方法
        userMapper.updateUser(user);
        userRoleMapper.deleteById(userId);
        //遍历添加角色信息
        for (String roleId : roleIds) {
            userRoleMapper.insertUserAndRole(userId,roleId);
        }
        //释放资源
        session.close();
    }

    //添加用户和对应角色信息
    @Override
    public void insertUserAndRole(AddUser addUser) {
        //获取会话对象
        SqlSession session = SqlSessionUtil.getSession();
        //获取接口代理对象
        UserMapper userMapper = session.getMapper(UserMapper.class);
        UserRoleMapper userRoleMapper = session.getMapper(UserRoleMapper.class);
        //创建对象存储数据
        User user = new User();
        user.setUsername(addUser.getUsername());
        user.setPassword(addUser.getPassword());
        user.setEmail(addUser.getEmail());
        user.setRemark(addUser.getRemark());
        //获取传来的角色id（需要添加的）
        List<String> roleIds = addUser.getRoleIds();
        //调用方法
        userMapper.insertUser(user);
        //遍历添加用户角色关系
        for (String roleId : roleIds) {
            //使用了自增主键回填
            userRoleMapper.insertUserAndRole(user.getId(),roleId);
        }
        //释放资源
        session.close();


    }

    //删除用户信息和该用户对应的角色信息
    @Override
    public void deleteById(String userId) {
        //获取会话对象
        SqlSession session = SqlSessionUtil.getSession();
        //获取接口代理对象
        UserMapper userMapper = session.getMapper(UserMapper.class);
        UserRoleMapper userRoleMapper = session.getMapper(UserRoleMapper.class);
        //调用方法  （需要先删除该用户角色中间表的所有信息）
        userRoleMapper.deleteById(userId);
        userMapper.deleteById(userId);
        //释放资源
        session.close();

    }
}
